<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${bp}/layui/css/layui.css" media="all">
    <script src="${bp}/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${bp}/layui/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="editUser" >
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">城市</label>
        <div class="layui-input-block">
            <input type="text" name="city" lay-verify="required" lay-reqtext="城市不能为空"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">生日</label>
        <div class="layui-input-block">
            <input type="date" name="birth" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;


        let  user = {}
        user.birth = "2022-2-1"
        // 获取父页面中定义的变量
        var usr = parent.usr;
        console.log(usr)
        //  格式化日期    1662998400000   ->   yyyy-MM-dd
        usr.birth = layui.util.toDateString(usr.birth,"yyyy-MM-dd")
        //表单数据的回显
        form.val("editUser",usr)
        //表单的重新渲染
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // 获取表单数据
            let  dt = JSON.stringify(data.field)    // {username:"tom",age:20,sex:"男"}  =>  {"username":"tom","age":20,"sex":"男"}
            // 发送异步请求添加用户信息
            console.log(dt)
            $.post("${bp}/user?method=update",{user:dt},function (res) {
                // 根据code 判定修改是否成功
                if(res.code == 0){
                    layer.msg(res.msg,{icon:1},function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        // 刷新父页面
                        parent.location.reload();
                    })
                }else{  // 修改失败
                    layer.msg(res.msg,{icon:2})
                }
            },'JSON')


            return false;
        });

    });
</script>
</body>
</html>
